/**
 * 
 */
package com.binhthuan.catechist.server.bus;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.binhthuan.catechist.server.domain.Area;
import com.binhthuan.catechist.server.util.HibernateUtil;

/**
 * @author dttgiang
 *
 */
public class AreaBUS extends AbstractBUS {

    public List<Area> getAllAreas() {
        return getAll(Area.class);
    }

    public Area getAreaById(int id) {
        return get(Area.class, id);
    }

    public boolean deleteArea(int id) {
        return delete(Area.class, id);
    }

    /**
     * Kiểm tra giáo khu có đang được sử dụng hay không.
     * @param id Mã giáo khu.
     * @return
     */
    public boolean isInUsed(int id) {
        return true;
    }

    public Area getAreaByName(String name) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        Area result = null;
        try{
            transaction = session.beginTransaction();
            Query query = session.createQuery("select c from Area c where c.name = :name");
            query.setString("name", name);
            result = (Area) query.uniqueResult();
            
            transaction.commit();
        } catch (HibernateException he) {
            he.printStackTrace();
            transaction.rollback();
        } finally {
            session.close();
        }
        
        return result;
    }
}
